package com.hzw.saas.service.user.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.user.ICustomerPRService;
import com.hzw.saas.api.user.bo.CustomerPRBO;
import com.hzw.saas.api.user.query.CustomerInfoUpdateQuery;
import com.hzw.saas.api.user.query.CustomerPRAddQuery;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.common.util.StringUtils;
import com.hzw.saas.service.user.mapper.CustomerInfoMapper;
import com.hzw.saas.service.user.model.CustomerInfo;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zzl
 * @since 08/23/2021
 */
@Service
@Slf4j
public class CustomerPRServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerPRService, IService<CustomerInfo> {

    @Override
    public CustomerPRBO save(CustomerPRAddQuery query) {
        this.assertValidParams2Save(query);
        CustomerInfo entity = MapperUtil.nf().map(query, CustomerInfo.class);
        this.initParams2Save(entity);
        super.save(entity);
        return MapperUtil.nf().map(entity, CustomerPRBO.class);
    }

    @Override
    public boolean update(CustomerInfoUpdateQuery query) {
        this.assertValidParams2Update(query);
        CustomerInfo entity = MapperUtil.nf().map(query, CustomerInfo.class);
        this.initParams2Update(entity);
        return super.updateById(entity);
    }

    @Override
    public boolean delete(String pid) {
        if (StrUtil.isBlank(pid))
            return false;
        return super.removeById(pid);
    }

    private void assertValidParams2Save(CustomerPRAddQuery query) {
        AssertUtil.assertThrow("客户姓名不可为空", StrUtil.isBlank(query.getCustomerName()));
        AssertUtil.assertThrow("节日信息不可为空", StrUtil.isBlank(query.getFestival()));
        AssertUtil.assertThrow("客户情况说明不可为空", StrUtil.isBlank(query.getDescription()));
        AssertUtil.assertThrow("填写人姓名不可为空", StrUtil.isBlank(query.getSubmitterName()));
        AssertUtil.assertThrow("鸿之微归属部门不可为空", StrUtil.isBlank(query.getHzwDepartment()));
    }

    private void initParams2Save(CustomerInfo entity2Save) {
        entity2Save.pureBase();
        entity2Save.setPid(SnowFlakeUtil.nextIdStr());
    }

    private void assertValidParams2Update(CustomerInfoUpdateQuery query) {
        AssertUtil.assertThrow("客户信息PID不可为空", StrUtil.isBlank(query.getPid()));
    }

    private void initParams2Update(CustomerInfo entity2Update) {
        if (StringUtils.isBlankText(entity2Update.getCustomerName()))
            entity2Update.setCustomerName(null);
        if (StringUtils.isBlankText(entity2Update.getFestival()))
            entity2Update.setFestival(null);
        if (StringUtils.isBlankText(entity2Update.getDescription()))
            entity2Update.setDescription(null);
        if (StringUtils.isBlankText(entity2Update.getSubmitterName()))
            entity2Update.setSubmitterName(null);
        if (StringUtils.isBlankText(entity2Update.getHzwDepartment()))
            entity2Update.setHzwDepartment(null);
    }
}
